﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PaggerApplication.Models;
using Alex.Library.Common;

namespace PaggerApplication.Controllers
{
    public class AjaxPaggerController : Controller
    {
        private static List<ProductModel> _products;
        private const int PAGESIZE = 12;

        static AjaxPaggerController()
        {

            _products = new List<ProductModel>();
            for (int i = 0; i < 1000; i++)
            {
                _products.Add(new ProductModel()
                {
                    ID = Guid.NewGuid().ToString(),
                    Name = "Product_" + (i + 1).ToString(),
                    Specification = "Spec_" + RandomHelper.Create(0, 1000, i).ToString(),
                    Price = RandomHelper.Create(100, 50000, i),
                    Count = RandomHelper.Create(1000, 10000, i)
                });
            }
        }

        private PageViewModel<ProductModel> GetModel(int? pageIndex, SearchModel searchModel)
        {
            int index = pageIndex ?? 0;
            double minCount = searchModel.MinCount ?? 0;
            double maxCount = searchModel.MaxCount ?? 0;
            IEnumerable<ProductModel> _data = null;
            if (maxCount > 0)
            {
                _data = _products.Where(p => p.Count >= minCount && p.Count <= maxCount);
            }
            else
            {
                _data = _products;
            }
            PageViewModel<ProductModel> model = new PageViewModel<ProductModel>()
            {
                Data = _data.OrderBy(p => p.ID).Skip(index * PAGESIZE).Take(PAGESIZE).ToList(),
                PageIndex = index,
                PageSize = PAGESIZE,
                RowCount = _data.Count()
            };
            return model;
        }


        #region PrevNext
        //
        // GET: /AjaxPagger/PrevNextGetIndex

        public ActionResult PrevNextGetIndex()
        {
            PageViewModel<ProductModel> model = new PageViewModel<ProductModel>()
            {
                Data = _products.OrderBy(p => p.ID).Take(PAGESIZE).ToList(),
                PageIndex = 0,
                PageSize = PAGESIZE,
                RowCount = _products.Count
            };
            return View(model);
        }

        //
        // GET: /AjaxPagger/PrevNextGet

        public ActionResult PrevNextGet(int? pageIndex)
        {
            int index = pageIndex ?? 0;
            PageViewModel<ProductModel> model = new PageViewModel<ProductModel>()
            {
                Data = _products.OrderBy(p => p.ID).Skip(index * PAGESIZE).Take(PAGESIZE).ToList(),
                PageIndex = index,
                PageSize = PAGESIZE,
                RowCount = _products.Count
            };
            return PartialView("_DataRow", model);
        }

        //
        //GET: /AjaxPagger/PrevNextPostIndex

        public ActionResult PrevNextPostIndex()
        {
            PageViewModel<ProductModel> model = new PageViewModel<ProductModel>()
            {
                Data = _products.OrderBy(p => p.ID).Take(PAGESIZE).ToList(),
                PageIndex = 0,
                PageSize = PAGESIZE,
                RowCount = _products.Count
            };
            ViewBag.SearchModel = new SearchModel();
            return View(model);
        }

        //
        //POST: /AjaxPagger/PrevNextPostIndex

        [HttpPost]
        public ActionResult PrevNextPostIndex(int? pageIndex, SearchModel searchModel)
        {
            PageViewModel<ProductModel> model = GetModel(pageIndex, searchModel);
            ViewBag.SearchModel = searchModel;
            return View(model);
        }

        //
        //POST: /AjaxPagger/PrevNextPost

        [HttpPost]
        public ActionResult PrevNextPost(int? pageIndex, SearchModel searchModel)
        {
            PageViewModel<ProductModel> model = GetModel(pageIndex, searchModel);
            ViewBag.SearchModel = searchModel;
            return PartialView("_DataRow", model);
        }

        #endregion

        #region Numeric

        //
        //GET: /AjaxPagger/NumericGetIndex

        public ActionResult NumericGetIndex()
        {
            PageViewModel<ProductModel> model = new PageViewModel<ProductModel>()
            {
                Data = _products.OrderBy(p => p.ID).Take(PAGESIZE).ToList(),
                PageIndex = 0,
                PageSize = PAGESIZE,
                RowCount = _products.Count
            };
            return View(model);
        }

        //
        // GET: AjaxPagger/NumericGet

        public ActionResult NumericGet(int? pageIndex)
        {
            int index = pageIndex ?? 0;
            PageViewModel<ProductModel> model = new PageViewModel<ProductModel>()
            {
                Data = _products.OrderBy(p => p.ID).Skip(index * PAGESIZE).Take(PAGESIZE).ToList(),
                PageIndex = index,
                PageSize = PAGESIZE,
                RowCount = _products.Count
            };
            return PartialView("_DataRow", model);
        }

        //
        //GET: /AjaxPagger/NumericPostIndex

        public ActionResult NumericPostIndex()
        {
            PageViewModel<ProductModel> model = new PageViewModel<ProductModel>()
            {
                Data = _products.OrderBy(p => p.ID).Take(PAGESIZE).ToList(),
                PageIndex = 0,
                PageSize = PAGESIZE,
                RowCount = _products.Count
            };
            ViewBag.SearchModel = new SearchModel();
            return View(model);
        }

        //
        //POST: /AjaxPagger/NumericPostIndex

        [HttpPost]
        public ActionResult NumericPostIndex(int? pageIndex, SearchModel searchModel)
        {
            PageViewModel<ProductModel> model = GetModel(pageIndex, searchModel);
            ViewBag.SearchModel = searchModel;
            return View(model);
        }
        
        //
        //POST: /AjaxPagger/NumericPost

        [HttpPost]
        public ActionResult NumericPost(int? pageIndex, SearchModel searchModel)
        {
            PageViewModel<ProductModel> model = GetModel(pageIndex, searchModel);
            ViewBag.SearchModel = searchModel;
            return PartialView("_DataRow", model);
        }

        #endregion

        #region More

        //
        //GET: /AjaxPagger/MoreGetIndex

        public ActionResult MoreGetIndex()
        {
            PageViewModel<ProductModel> model = new PageViewModel<ProductModel>()
            {
                Data = _products.OrderBy(p => p.ID).Take(PAGESIZE).ToList(),
                PageIndex = 0,
                PageSize = PAGESIZE,
                RowCount = _products.Count
            };
            return View(model);
        }

        //
        //GET: /AjaxPagger/MoreGet

        public ActionResult MoreGet(int? pageIndex)
        {
            int index = pageIndex ?? 0;
            PageViewModel<ProductModel> model = new PageViewModel<ProductModel>()
            {
                Data = _products.OrderBy(p => p.ID).Skip(index * PAGESIZE).Take(PAGESIZE).ToList(),
                PageIndex = index,
                PageSize = PAGESIZE,
                RowCount = _products.Count
            };
            return PartialView("_DataRow", model);
        }

        //
        //GET: /AjaxPagger/MorePostIndex

        public ActionResult MorePostIndex()
        {
            PageViewModel<ProductModel> model = new PageViewModel<ProductModel>()
            {
                Data = _products.OrderBy(p => p.ID).Take(PAGESIZE).ToList(),
                PageIndex = 0,
                PageSize = PAGESIZE,
                RowCount = _products.Count
            };
            ViewBag.SearchModel = new SearchModel();
            return View(model);
        }
        
        //
        //POST: /AjaxPagger/MorePostIndex

        [HttpPost]
        public ActionResult MorePostIndex(int? pageIndex, SearchModel searchModel)
        {
            PageViewModel<ProductModel> model = GetModel(pageIndex, searchModel);
            ViewBag.SearchModel = searchModel;
            return View(model);
        }

        //
        //POST: /AjaxPagger/MorePost

        [HttpPost]
        public ActionResult MorePost(int? pageIndex, SearchModel searchModel)
        {
            PageViewModel<ProductModel> model = GetModel(pageIndex, searchModel);
            ViewBag.SearchModel = searchModel;
            return PartialView("_DataRow", model);
        }

        #endregion
    }
}
